{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/52_cartoee_gif.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) and [cartopy](https://scitools.org.uk/cartopy/docs/latest/installing.html#installing) if needed. Keep in mind that cartopy can be challenging to install. If you are unable to install cartopy on your computer, you can try Google Colab with this the [notebook example](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/cartoee_colab.ipynb). \n",
    "\n",
    "See below the commands to install cartopy and geemap using conda/mamba:\n",
    "\n",
    "```\n",
    "conda create -n carto python=3.8\n",
    "conda activate carto\n",
    "conda install mamba -c conda-forge\n",
    "mamba install cartopy scipy -c conda-forge\n",
    "mamba install geemap -c conda-forge\n",
    "jupyter notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "# How to create timelapse animations using cartoee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ee\n",
    "import geemap\n",
    "from geemap import cartoee\n",
    "\n",
    "%pylab inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Create an ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "lon = -115.1585\n",
    "lat = 36.1500\n",
    "start_year = 1984\n",
    "end_year = 2011\n",
    "\n",
    "point = ee.Geometry.Point(lon, lat)\n",
    "years = ee.List.sequence(start_year, end_year)\n",
    "\n",
    "\n",
    "def get_best_image(year):\n",
    "    start_date = ee.Date.fromYMD(year, 1, 1)\n",
    "    end_date = ee.Date.fromYMD(year, 12, 31)\n",
    "    image = (\n",
    "        ee.ImageCollection(\"LANDSAT/LT05/C01/T1_SR\")\n",
    "        .filterBounds(point)\n",
    "        .filterDate(start_date, end_date)\n",
    "        .sort(\"CLOUD_COVER\")\n",
    "        .first()\n",
    "    )\n",
    "    return ee.Image(image)\n",
    "\n",
    "\n",
    "collection = ee.ImageCollection(years.map(get_best_image))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "## Display a sample image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_params = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"min\": 0, \"max\": 5000}\n",
    "\n",
    "image = ee.Image(collection.first())\n",
    "Map.addLayer(image, vis_params, \"First image\")\n",
    "Map.setCenter(lon, lat, 8)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Get a sample output image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "w = 0.4\n",
    "h = 0.3\n",
    "\n",
    "region = [lon + w, lat - h, lon - w, lat + h]\n",
    "\n",
    "fig = plt.figure(figsize=(10, 8))\n",
    "\n",
    "# use cartoee to get a map\n",
    "ax = cartoee.get_map(image, region=region, vis_params=vis_params)\n",
    "\n",
    "# add gridlines to the map at a specified interval\n",
    "cartoee.add_gridlines(ax, interval=[0.2, 0.2], linestyle=\":\")\n",
    "\n",
    "# add north arrow\n",
    "north_arrow_dict = {\n",
    "    \"text\": \"N\",\n",
    "    \"xy\": (0.1, 0.3),\n",
    "    \"arrow_length\": 0.15,\n",
    "    \"text_color\": \"white\",\n",
    "    \"arrow_color\": \"white\",\n",
    "    \"fontsize\": 20,\n",
    "    \"width\": 5,\n",
    "    \"headwidth\": 15,\n",
    "    \"ha\": \"center\",\n",
    "    \"va\": \"center\",\n",
    "}\n",
    "cartoee.add_north_arrow(ax, **north_arrow_dict)\n",
    "\n",
    "# add scale bar\n",
    "scale_bar_dict = {\n",
    "    \"length\": 10,\n",
    "    \"xy\": (0.1, 0.05),\n",
    "    \"linewidth\": 3,\n",
    "    \"fontsize\": 20,\n",
    "    \"color\": \"white\",\n",
    "    \"unit\": \"km\",\n",
    "    \"ha\": \"center\",\n",
    "    \"va\": \"bottom\",\n",
    "}\n",
    "cartoee.add_scale_bar_lite(ax, **scale_bar_dict)\n",
    "\n",
    "ax.set_title(label=\"Las Vegas, NV\", fontsize=15)\n",
    "\n",
    "show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "## Create timelapse animations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "cartoee.get_image_collection_gif(\n",
    "    ee_ic=collection,\n",
    "    out_dir=os.path.expanduser(\"~/Downloads/timelapse\"),\n",
    "    out_gif=\"animation.gif\",\n",
    "    vis_params=vis_params,\n",
    "    region=region,\n",
    "    fps=5,\n",
    "    mp4=True,\n",
    "    grid_interval=(0.2, 0.2),\n",
    "    plot_title=\"Las Vegas, NV\",\n",
    "    date_format=\"YYYY-MM-dd\",\n",
    "    fig_size=(10, 8),\n",
    "    dpi_plot=100,\n",
    "    file_format=\"png\",\n",
    "    north_arrow_dict=north_arrow_dict,\n",
    "    scale_bar_dict=scale_bar_dict,\n",
    "    verbose=True,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
